package cn.group7.padserver.controller;

import cn.group7.padserver.common.Flag;
import cn.group7.padserver.controller.responseformat.Code;
import cn.group7.padserver.controller.responseformat.Result;
import cn.group7.padserver.pojo.Address;
import cn.group7.padserver.service.AddressService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/5 上午11:50
 */
@RestController
@RequestMapping("/addresses")
public class AddressController {

    @Resource
    private AddressService addressService;

    /**
     * 添加地址
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        boolean add = addressService.add(address);
        return Result.builder().flag(add ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .code(add ? Code.ADD_SUCCESS.getCode() : Code.ADD_FAIL.getCode())
                .message(add ? Code.ADD_SUCCESS.getInfo() : Code.ADD_FAIL.getInfo())
                .build();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update() {
        boolean update = addressService.update();
        return Result.builder().flag(update ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .code(update ? Code.UPDATE_SUCCESS.getCode() : Code.UPDATE_FAIL.getCode())
                .message(update ? Code.UPDATE_SUCCESS.getInfo() : Code.UPDATE_FAIL.getInfo())
                .build();
    }



    /**
     * 根据id删除地址
     */
    @RequestMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean delete = addressService.delete(id);
        return Result.builder().flag(delete ? Flag.SUCCESS.isCode() : Flag.FAIL.isCode())
                .code(delete ? Code.DELETE_SUCCESS.getCode() : Code.DELETE_FAIL.getCode())
                .message(delete ? Code.DELETE_SUCCESS.getInfo() : Code.DELETE_FAIL.getInfo()).build();
    }

    /**
     * 根据登录的用户查询所有地址
     */
    @RequestMapping("/selectAll")
    public Result selectAll() {
        return Result.builder().flag(Flag.SUCCESS.isCode())
                .code(Code.QUERY_SUCCESS.getCode())
                .message(Code.QUERY_SUCCESS.getInfo())
                .data(addressService.selectAll())
                .build();
    }
}
